package com.tiger.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.tiger.project.mapper.MaintenanceApplyMapper;
import com.tiger.project.mapper.PatrolRecordMapper;
import com.tiger.project.service.IProjectStatisticsService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目统计业务逻辑处理接口实现类
 *
 * @author Lyn 宋开放
 * @date 2023-01-09 10:13
 **/
@Service
public class ProjectStatisticsServiceImpl implements IProjectStatisticsService {
    private final int beginYear = 2017;
    private final PatrolRecordMapper patrolRecordMapper;

    private final MaintenanceApplyMapper maintenanceApplyMapper;

    public ProjectStatisticsServiceImpl(PatrolRecordMapper patrolRecordMapper, MaintenanceApplyMapper maintenanceApplyMapper) {
        this.patrolRecordMapper = patrolRecordMapper;
        this.maintenanceApplyMapper = maintenanceApplyMapper;
    }

    @Override
    public List<Map<String, Object>> getPatrolStatisticsLastYear(Long companyId) {

        return this.patrolRecordMapper.selectPatrolStatisticsLastYear(companyId);
    }

    @Override
    public Map<String, Object> getProjectTotalStatistics(Long deptId) {
        return this.patrolRecordMapper.selectProjectTotalStatistics(deptId);
    }

    @Override
    public List<Map<String, Object>> getMaintenanceStatisticsByYear(String year, Long companyId) {
        return this.maintenanceApplyMapper.selectMaintenanceStatisticsByYear(year, companyId);
    }

    @Override
    public List<Map<String, Object>> getMaintenanceSummary(Long companyId) {
        int currentYear = DateUtil.date().year();
        List<Map<String, Object>> maps = this.maintenanceApplyMapper.selectMaintenanceSummary(companyId);
        int step = currentYear - beginYear;
        Map<String, Object> map = null;
        for (int i = 0; i < step; i++) {
            int currentLoopYear = beginYear+i;
            if (maps.stream().noneMatch(m -> m.get("year").equals(currentLoopYear))) {
                map = new HashMap<String, Object>(4) {
                    {
                        put("year", currentLoopYear);
                        put("total",0);
                        put("land",0);
                        put("house",0);
                    }
                };
                maps.add(map);
            }
        }
        return maps;
    }

    @Override
    public List<Map<String, Object>> getMaintenanceLast12MonthStatistics(Long companyId) {
        return this.maintenanceApplyMapper.selectLast12MonthStatistics(companyId);
    }

    @Override
    public Map<String, Object> getMaintenanceTotalSummary(Long deptId) {

        return this.maintenanceApplyMapper.selectmaintenanceTotalSummary(deptId);
    }
}
